package Hash;

import java.util.Arrays;

public class 两个数组的交集 {
    public int[] intersection(int[] nums1, int[] nums2) {
        /*Set<Integer> hashSet1 = new HashSet<>();
        Set<Integer> hashSet2 = new HashSet<>();
        for (int i : nums1) hashSet1.add(i);
        for (int i : nums2) if (hashSet1.contains(i)) hashSet2.add(i);
        int[] result = new int[hashSet2.size()];
        int index = 0;
        for (int i : hashSet2) result[index++] = i;
        return result;*/

        int[] a = new int[1001], b = new int[1001];
        int index = 0;
        for (int i : nums1) a[i] = 1;
        for (int j : nums2)
            if (a[j] == 1) {
                b[index++] = j;
                a[j]++;
            }
        return Arrays.copyOf(b, index);
    }
}
